#ifndef __APP_BUFFER_H__
#define __APP_BUFFER_H__

#include "../thirdparty/log/log.h"
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
    unsigned char *buffer;   // 缓冲区
    int buffer_len; // 当前缓冲区数据长度
    int buffer_size;// 缓冲区实际大小
} Base_buffer_t;

typedef struct
{
    Base_buffer_t* buffers[2];    // 双缓冲区
    int read_index;              // 读缓存区索引
    int write_index;             // 写缓存区索引
    pthread_mutex_t read_mutex;  // 读互斥锁
    pthread_mutex_t write_mutex; // 写互斥锁
} Double_buffer_t;

/**
 * @brief 初始化双缓冲区
 * @param buffer_len 单个缓冲区大小
 * @return 双缓冲区地址
 */
Double_buffer_t* app_buffer_init(int buffer_len);

/**
 * @brief 写入数据到双缓冲区
 * @param double_buffer 双缓冲区
 * @param data 写入的数据
 * @param data_len 写入数据的具体长度
 * @return 0:成功，-1:失败
 */
int app_buffer_write(Double_buffer_t *double_buffer, unsigned char *data, unsigned char data_len);

/**
 * @brief 从双缓冲区读取数据
 * @param double_buffer 双缓冲区
 * @param data 读取的数据容器
 * @param data_size 读取数据的容器的大小
 * @return 读取到的数据长度
 */
int app_buffer_read(Double_buffer_t *double_buffer, unsigned char *data, unsigned char data_size);

/**
 * @brief 释放双缓冲区
 * @param double_buffer 双缓冲区
 */
void app_buffer_free(Double_buffer_t *double_buffer);

#endif /* __APP_BUFFER_H__ */